//
//  UILabel+attributedString.m
//  PhotoSelector
//
//  Created by 李李善 on 2017/11/10.
//  Copyright © 2017年 洪雯. All rights reserved.
//

#import "NSMutableAttributedString+attributedString.h"

@implementation NSMutableAttributedString (attributedString)
+(NSMutableAttributedString *)onAttributedStringForText:(NSString *)text LabelAtt:(LabelAttributedString)labelAtt
{
    NSMutableAttributedString *att = [[NSMutableAttributedString alloc]initWithString:text];
    if (labelAtt) {
        labelAtt(att);
    }
    return att;
}


#pragma mark - 文本颜色属性
- (void)addAttributeTextColor:(UIColor*)color
{
    [self addAttributeTextColor:color range:NSMakeRange(0, [self length])];
}

- (void)addAttributeTextColor:(UIColor*)color range:(NSRange)range
{
    if (color)
    {
        [self removeAttribute:NSForegroundColorAttributeName range:range];
        
        [self addAttribute:NSForegroundColorAttributeName
                     value:color
                     range:range];
    }
    
}

#pragma mark - 文本字体属性
- (void)addAttributeFont:(UIFont *)font
{
    [self addAttributeFont:font range:NSMakeRange(0, [self length])];
}

- (void)addAttributeFont:(UIFont *)font range:(NSRange)range
{
    if (font)
    {
        [self removeAttribute:NSFontAttributeName range:range];
        
        CTFontRef fontRef = CTFontCreateWithName((CFStringRef)font.fontName, font.pointSize, nil);
        if (nil != fontRef)
        {
            [self addAttribute:NSFontAttributeName value:(__bridge id)fontRef range:range];
            CFRelease(fontRef);
        }
    }
}

#pragma mark - 文本字符间隔属性
- (void)addAttributeCharacterSpacing:(unichar)characterSpacing
{
    [self addAttributeCharacterSpacing:characterSpacing range:NSMakeRange(0, self.length)];
}

- (void)addAttributeCharacterSpacing:(unichar)characterSpacing range:(NSRange)range
{
    [self removeAttribute:NSKernAttributeName range:range];
    
    CFNumberRef num =  CFNumberCreate(kCFAllocatorDefault,kCFNumberSInt8Type,&characterSpacing);
    [self addAttribute:NSKernAttributeName value:(__bridge id)num range:range];
    CFRelease(num);
}

#pragma mark - 文本下划线属性
- (void)addAttributeUnderlineStyle:(CTUnderlineStyle)style
                          modifier:(CTUnderlineStyleModifiers)modifier
{
    [self addAttributeUnderlineStyle:style
                            modifier:modifier
                               range:NSMakeRange(0, self.length)];
}

- (void)addAttributeUnderlineStyle:(CTUnderlineStyle)style
                          modifier:(CTUnderlineStyleModifiers)modifier
                             range:(NSRange)range
{
    [self removeAttribute:NSUnderlineColorAttributeName range:range];
    
    if (style != kCTUnderlineStyleNone) {
        [self addAttribute:(NSString *)kCTUnderlineStyleAttributeName
                     value:[NSNumber numberWithInt:(style|modifier)]
                     range:range];
    }
    
}

#pragma mark - 文本段落样式属性
- (void)addAttributeAlignmentStyle:(CTTextAlignment)textAlignment
                    lineSpaceStyle:(CGFloat)linesSpacing
                    lineBreakStyle:(CTLineBreakMode)lineBreakMode
{
    [self addAttributeAlignmentStyle:textAlignment lineSpaceStyle:linesSpacing lineBreakStyle:lineBreakMode range:NSMakeRange(0, self.length)];
}

- (void)addAttributeAlignmentStyle:(CTTextAlignment)textAlignment
                    lineSpaceStyle:(CGFloat)linesSpacing
                    lineBreakStyle:(CTLineBreakMode)lineBreakMode
                             range:(NSRange)range
{
    [self removeAttribute:(id)kCTParagraphStyleAttributeName range:range];
    
    // 创建文本对齐方式
    CTParagraphStyleSetting alignmentStyle;
    alignmentStyle.spec = kCTParagraphStyleSpecifierAlignment;//指定为对齐属性
    alignmentStyle.valueSize = sizeof(textAlignment);
    alignmentStyle.value = &textAlignment;
    
    // 创建文本行间距
    CTParagraphStyleSetting lineSpaceStyle;
    lineSpaceStyle.spec = kCTParagraphStyleSpecifierLineSpacing;//指定为行间距属性
    lineSpaceStyle.valueSize = sizeof(linesSpacing);
    lineSpaceStyle.value = &linesSpacing;
    
    //换行模式
    CTParagraphStyleSetting lineBreakStyle;
    lineBreakStyle.spec = kCTParagraphStyleSpecifierLineBreakMode;
    lineBreakStyle.value = &lineBreakMode;
    lineBreakStyle.valueSize = sizeof(lineBreakMode);
    
    // 创建样式数组
    CTParagraphStyleSetting settings[] = {alignmentStyle,lineSpaceStyle ,lineBreakStyle};
    CTParagraphStyleRef paragraphStyle = CTParagraphStyleCreate(settings, sizeof(settings) / sizeof(settings[0]));	// 设置样式
    
    // 设置段落属性
    [self addAttribute:(id)kCTParagraphStyleAttributeName value:(id)CFBridgingRelease(paragraphStyle) range:range];
}



@end
